package com.fishery.customization.controller;

import com.fishery.customization.entity.FixedLabel;
import com.fishery.customization.service.FixedLabelService;
import com.fishery.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/customization/admin/label")
@Api(tags = "固定类型标签管理模块")
@CrossOrigin
public class FixedLabelController {

	@Autowired
	private FixedLabelService fixedLabelService;

	@GetMapping("/{type}")
	@ApiOperation("通过类型查询固定类型的标签")
	public Result searchAllFixedLabels(@PathVariable(name = "type", required = true)
									   @ApiParam(value = "type", required = true) String type) {
		if (StringUtils.isEmpty(type)) {
			return Result.error("查询失败");
		}
		return Result.success().data(fixedLabelService.findFixedLabelById(type));
	}

	@GetMapping
	@ApiOperation("查询所有固定类型的标签")
	public Result searchAllFixedLabels() {
		return Result.success().data(fixedLabelService.findAllFixedLabels());
	}

	@PostMapping
	@ApiOperation("增添固定类型的标签")
	public Result addFixedLabel(@RequestBody @ApiParam(value = "fixedLabel") FixedLabel fixedLabel) {
		if (fixedLabel == null
				|| StringUtils.isEmpty(fixedLabel.getId())
				|| StringUtils.isEmpty(fixedLabel.getName())
				|| fixedLabel.getLabels() == null
				|| fixedLabel.getLabels().isEmpty()) {
			return Result.error("增添失败");
		}
		return fixedLabelService.saveFixedLabel(fixedLabel)
				? Result.success("增添成功") : Result.error("增添失败");
	}

	@DeleteMapping("/{id}")
	@ApiOperation("删除固定类型的标签")
	public Result deleteFixedLabel(@PathVariable(name = "id",required = true)
								   @ApiParam(value = "id", required = true) String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error("删除失败");
		}
		return fixedLabelService.removeFixedLabelById(id)
				? Result.success("删除成功") : Result.error("删除失败");
	}

	@PutMapping
	@ApiOperation("修改固定类型的标签")
	public Result updateFixedLabel(@RequestBody @ApiParam(value = "fixedLabel") FixedLabel fixedLabel) {
		if (fixedLabel == null
				|| StringUtils.isEmpty(fixedLabel.getId())) {
			return Result.error("修改失败");
		}
		return fixedLabelService.updateFixedLabelById(fixedLabel)
				? Result.success("修改成功") : Result.error("修改失败");
	}

}
